package com.xidian.zhiancloud.controller.chemicalsManagement.wastePart;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.WasteInfo;
import com.xidian.zhiancloud.qo.chemicalsManagement.WasteInfoQO;
import com.xidian.zhiancloud.service.chemicalsManagement.chemicalManager.wastePart.WasteInfoService;
import com.xidian.zhiancloud.vo.chemicalsManagement.WasteInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * 危化品管理 废弃物管理 废弃物库存管理
 * @Author: Egoist
 * @Date: 2020/11/10 17:31
 */
@RestController
@RequestMapping("/WasteInfo")
public class WasteInfoController extends BaseController {

    @Autowired
    private WasteInfoService wasteInfoService;

    /**
     * 增加废弃物库存信息(废弃物名称不能重复)
     *
     * @param wasteInfo 废弃物库存信息对象
     * @return 返回是否添加成功
     */
    @PostMapping("/add")
    public ResultDTO<Boolean> add(WasteInfo wasteInfo) {
        wasteInfo.setCreateTime(new Date(System.currentTimeMillis()));
        wasteInfo.setCompanyId(getSessionUserCompanyId());
        return wasteInfoService.saveInfo(wasteInfo);
    }

    /**
     * 根据id获取废弃物库存信息
     *
     * @param id 废弃物库存对象id
     * @return 废弃物库存对象
     */
    @GetMapping("/getById")
    public ResultDTO<WasteInfoVO> getById(Long id) {
        return ResultDTO.successOf(wasteInfoService.getVOById(id), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id删除废弃物库存信息
     *
     * @param ids ids数组
     * @return 返回是否删除成功
     */
    @PostMapping("/deleteByIds")
    public ResultDTO<Boolean> deleteByIds(Long[] ids) {

        return ResultDTO.successOf(wasteInfoService.removeByIds(Arrays.asList(ids)), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id修改废弃物库存对象(废弃物名称不能重复)
     * 只传入 id 与 用户修改过的内容
     * @param wasteInfo 废弃物库存对象
     * @return 返回是是否修改成功
     */
    @PostMapping("/patchById")
    public ResultDTO<Boolean> patchById(WasteInfo wasteInfo) {
        wasteInfo.setCompanyId(null);
        Long companyId = getSessionUserCompanyId();
        return wasteInfoService.updateByIdCheckName(wasteInfo, companyId);
    }

    /**
     * 根据品名获取单位
     * @param name 废弃物品名
     * @return 单位
     */
    @GetMapping("/getUnitByName")
    public ResultDTO<String> getUnitByName(String name) {
        return ResultDTO.successOf(wasteInfoService.getUnitByName(name), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 获取废弃物名称列表
     * @return 废弃物名称列表
     */
    @GetMapping("/getWasteNameList")
    public ResultDTO<Map<Long, String>> getWasteNameList() {
        Long companyId = getSessionUserCompanyId();
        return ResultDTO.successOf(wasteInfoService.getName(companyId), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 列表查询
     *
     * @param wasteInfoQO 查询参数
     * @return 返回废弃物库存列表
     */
    @GetMapping("/getListByQO")
    public ResultDTO<IPage<WasteInfoVO>> getListByQueryObject(WasteInfoQO wasteInfoQO){
        if (wasteInfoQO.getCurrent() == null || wasteInfoQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        wasteInfoQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(wasteInfoService.getListByQueryObject(wasteInfoQO), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id获取计量单位、出库人、类别
     * @param id 废弃物品名
     * @return 单位
     */
    @GetMapping("/getInfoById")
    public ResultDTO<Map<String, String>> getInfoById(Long id) {
        Long userId = getSessionUserid();
        return wasteInfoService.getInfoById(id, userId);
    }
}
